﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;
using UncleBobApplicantWebService.ApplicantWebService;


namespace UncleBobApplicantWebService.Controllers
{
    public class RegisterController : Controller
    {
        //----------------------------------------------
        //----------------------------------------------
        private static RegisterModel applicant = new RegisterModel();
        private UncleBobApplicantWebService.ApplicantWebService.WebServiceClient mApplicantClient = new UncleBobApplicantWebService.ApplicantWebService.WebServiceClient();


        //**************************************************
        //Display register view
        //**************************************************
        public ActionResult Register()
        {
            if (applicant.Username != null)
                applicant = new RegisterModel();
            return View(applicant);
        }


        //**************************************************
        //Display preview view
        //**************************************************
        public ActionResult RegisterPreview()
        {
            return View(applicant);
        }


        //**************************************************
        //Form action handle
        //**************************************************
        public ActionResult FormAction(RegisterModel model, FormCollection collection)
        {
            if (collection["cancel"] != null)
                return RedirectToAction("Index", "Welcome");
            else
            {
                // Delete previous applicant
                if (applicant.Username != null)
                {
                    applicant = null;
                    applicant = new RegisterModel();
                }

                applicant.Username = collection["txtName"];
                applicant.Password = collection["txtPWord"];
                applicant.FirstName = collection["txtFirstN"];
                applicant.LastName = collection["txtLastN"];
                applicant.MI = collection["txtMiddle"];
                applicant.PhoneNumber = collection["txtPhone"];
                applicant.Email = collection["txtEmail"];
                applicant.Address = collection["txtAddress"];

                Session["User"] = applicant;
            }

            return RedirectToAction("RegisterPreview", "Register");
        }


        public ActionResult PreviewFormAction(FormCollection collection)
        {
            if (collection["cancel"] != null)
                return RedirectToAction("Index", "Welcome");
            else if (collection["back"] != null)
                return RedirectToAction("Register", "Register");
            else
            {
                Message msg = new Message();
                //msg.Category = MessageTypes.SERVICE_VALIDATE_USER;
                msg.Category = MessageTypes.SERVICE_REGISTER_NEW_USER;
                msg.Data = (Object)applicant;
                msg = mApplicantClient.RequestService(msg);

                switch (msg.Result)
                {
                    case true:
                        return RedirectToAction("MainMenu", "ApplicantMainMenu");

                    case false:
                        return RedirectToAction("Error", "Error");
                }
            }

            return RedirectToAction("MainMenu", "ApplicantMainMenu");
        }
    }
}
